﻿using System.IO;
using System.Text;

using NUnit.Framework;

using Uhi.Libraries.Test;
using Uhi.Libraries.Test.Files;

using Uhi.Applications.FileAid.Model;

namespace FileAidTest.Model
{
    [TestFixture]
    public class RecordInfoTest
    {
        #region Filename

        #region 正常ケース
        [Test]
        public void Filename_正常ケース()
        {
            var rec = new RecordInfo("foo", 10, 100, 1);
            rec.Filename.Is("foo");
        }
        #endregion

        #endregion

        #region Offset

        #region 正常ケース
        [Test]
        public void Offset_正常ケース()
        {
            var rec = new RecordInfo("foo", 10, 100, 1);
            rec.Offset.Is(10);
        }
        #endregion

        #endregion

        #region Length

        #region 正常ケース
        [Test]
        public void Length_正常ケース()
        {
            var rec = new RecordInfo("foo", 10, 100, 1);
            rec.Length.Is(100);
        }
        #endregion

        #endregion

        #region Line

        #region 正常ケース
        [Test]
        public void Line_正常ケース()
        {
            var rec = new RecordInfo("foo", 10, 100, 1);
            rec.Line.Is(1);
        }
        #endregion

        #endregion

        #region IsEdited

        #region 正常ケース
        [Test]
        public void IsEdited_正常ケース()
        {
            var rec = new RecordInfo("foo", 10, 100, 1);
            rec.IsEdited = true;
            rec.IsEdited.Is(true);
        }
        #endregion

        #endregion

        #region Record

        #region 正常ケース
        [Test]
        public void Record_正常ケース()
        {
            var data = "1111122222333334444455555";
            var dir = Path.Combine(Path.GetTempPath(), "RecordInfoTest");
            var file = "foo.txt";
            var txt = new Txt(dir, file, data);

            using (txt)
            {
                txt.Setup();

                var rec = new RecordInfo(Path.Combine(dir, file), 10, 5, 2);
                rec.Record.Is(Encoding.Default.GetBytes("33333"));

                var bytes = Encoding.Default.GetBytes("ccccc");
                rec.Record = bytes;
                rec.Record.Is(bytes);
            }
        }
        #endregion

        #region レコード長がゼロ
        [Test]
        public void Record_レコード長がゼロ()
        {
            var data = "1111122222333334444455555";
            var dir = Path.Combine(Path.GetTempPath(), "RecordInfoTest");
            var file = "foo.txt";
            var txt = new Txt(dir, file, data);

            using (txt)
            {
                txt.Setup();

                var rec = new RecordInfo(Path.Combine(dir, file), 10, 0, 2);
                rec.Record.Is(Encoding.Default.GetBytes(""));
            }
        }
        #endregion

        #endregion

        #region Save

        #region 正常ケース
        [Test]
        public void Save_正常ケース()
        {
            var data = "1111122222333334444455555";
            var dir = Path.Combine(Path.GetTempPath(), "RecordInfoTest");
            var file = "foo.txt";
            var txt = new Txt(dir, file, data);

            using (txt)
            {
                txt.Setup();

                var rec = new RecordInfo(Path.Combine(dir, file), 10, 5, 2);
                var bytes = Encoding.Default.GetBytes("ccccc");
                rec.Record = bytes;
                rec.Save();

                File.ReadAllText(Path.Combine(dir, file)).Is("1111122222ccccc4444455555");
            }
        }
        #endregion

        #endregion

        #region SaveAs

        #region 正常ケース
        [Test]
        public void SaveAs_正常ケース()
        {
            var data = "1111122222333334444455555";
            var dir = Path.Combine(Path.GetTempPath(), "RecordInfoTest");
            var file1 = "foo.txt";
            var file2 = "bar.txt";
            var txt1 = new Txt(dir, file1, data);
            var txt2 = new Txt(dir, file2, "aaaaa");

            using (txt1)
            using (txt2)
            {
                txt1.Setup();
                txt2.Setup();

                var rec = new RecordInfo(Path.Combine(dir, file1), 10, 5, 2);
                var bytes = Encoding.Default.GetBytes("ccccc");
                rec.Record = bytes;
                rec.SaveAs(Path.Combine(dir, file2));

                File.ReadAllText(Path.Combine(dir, file1)).Is(data);
                File.ReadAllText(Path.Combine(dir, file2)).Is("aaaaa\0\0\0\0\0ccccc");
            }
        }
        #endregion

        #region ファイルが存在しない
        [Test]
        public void SaveAs_ファイルが存在しない()
        {
            var data = "1111122222333334444455555";
            var dir = Path.Combine(Path.GetTempPath(), "RecordInfoTest");
            var file1 = "foo.txt";
            var file2 = "bar.txt";
            var txt1 = new Txt(dir, file1, data);
            var txt2 = new Txt(dir, file2, "aaaaa");

            using (txt1)
            using (txt2)
            {
                txt1.Setup();
                txt2.Setup();

                File.Delete(Path.Combine(dir, file2));

                var rec = new RecordInfo(Path.Combine(dir, file1), 10, 5, 2);
                var bytes = Encoding.Default.GetBytes("ccccc");
                rec.Record = bytes;
                rec.SaveAs(Path.Combine(dir, file2));

                File.ReadAllText(Path.Combine(dir, file1)).Is(data);
                File.ReadAllText(Path.Combine(dir, file2)).Is("\0\0\0\0\0\0\0\0\0\0ccccc");
            }
        }
        #endregion

        #endregion
    }
}
